package kr.pe.mybjj;
import org.json.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class QnABoardServlet
 */
@WebServlet("/QnABoard")
public class QnABoardServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public QnABoardServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		String page = null;
		String sort = null;
		String process = null;
		
		request.setCharacterEncoding("UTF-8");
		process = request.getParameter("process");
		
		ArrayList<QnADTO> list = null;
		
		switch(process)
		{
		case "insert":
			QnAInsert insert = new QnAInsert();
			insert.execute(request, response);
			break;
		case "select":
			page = request.getParameter("page");
			sort = request.getParameter("sort");
			break;
		case "delete":
			break;
		case "search":
			QnASearch service = new QnASearch();
			list = service.execute(request, response);
			
			break;
		default:
			break;
		}
				
		if( process.equals("search"))
		{
			JSONObject json      = new JSONObject();
			JSONArray  addresses = new JSONArray();
			JSONObject address;
			try
			{
			   int count = 15;

			   for( QnADTO dto : list )
				{
			       address = new JSONObject();
			       address.put("no", dto.getno());
			       address.put("username", dto.getuserName());
			       address.put("password", dto.getpassword());
			       address.put("title", dto.gettitle());
			       address.put("content", dto.getcontent());
			       address.put("createdate", dto.getcreatedate());
			       addresses.put(address);
			       
			   }
			   json.put("Addresses", addresses);
			}
			catch (JSONException jse)
			{ 

			}
			response.setContentType("application/json");
			response.setCharacterEncoding("UTF-8");
			response.getWriter().write(json.toString());
/*			
			response.setContentType("text/html; charset=UTF-8");
			response.setContentType("text/plain");
			response.setCharacterEncoding("UTF-8");
			PrintWriter html = response.getWriter();
			for( QnADTO dto : list )
			{
				html.write("<label>작성자 <br /><input type='text' name='name' value='" + dto.getuserName() + "' readonly></label><br />"
							+ "<label>제목<br /><input name='title' type='text' value='" + dto.gettitle() + "' readonly></label><br />"
							+ "<label>작성일<br /><input name='date' type='text' value='" + dto.getcreatedate() + "' readonly></label><br />"
							+ "<label>내용<br /><textarea name='content'  rows='5' cols='24' readonly>"+ dto.getcontent() +"</textarea></label><br />"
							);
			}
*/		
		}
		else
		{
			response.setContentType("text/html; charset=UTF-8");
			response.setContentType("text/plain");
			response.setCharacterEncoding("UTF-8");
			PrintWriter html = response.getWriter();
			
			QnASelect select = new QnASelect();
			list = select.execute(request, response);
			
			
			
			html.write("<br><br><div><a href='#' class='button black' id='create-qna'>문의 작성</a></div>");
			html.write("<div class='divtable'><table style='border-collapse: collapse;border-spacing: 0px;line-height: 1;'>"
					+ "<tbody><tr><td style='width:5%;'>No</td><td>제       목</td><td style='width:10%;'>등록자</td><td style='width:20%;'>등록일자</td></tr>");
			
			for( QnADTO dto : list )
			{
				html.write("<tr><td>" + dto.getno() + "</td><td><a href='#' class='opencontent' data-listno='" + dto.getno() + "'><span>" + dto.gettitle() + "</span></a></td><td>" + dto.getuserName() + "</td><td>" + dto.getcreatedate() + "</td></tr>");
			}
			html.write("</tbody></table></div>");
			
			
	/*		html.write("<div id='dialog' title='문의하기 양식'>"
					+ "<form action='' method='post'>"
					+ "<label>작성자:</label>"
					+ "<input id='name' name='name' type='text'><br>"
					+ "<label>비밀번호:</label>"
					+ "<input id='passwd' name='passwd' type='password'><br>"
					+ "<label>제목:</label>"
					+ "<input id='title' name='title' type='text'><br>"
					+ "<label>내용:</label>"
					+ "<textarea id='content'  rows='5' cols='24'></textarea>"
					+ "<div><a href='#' class='button black' id='btn_submit' style='width: 60px;height: 20px;padding-top: 7px;'>전 송</a></div>"
					+ "</form></div>");*/
			
			
			html.write("<script src='jquery/dialog.js'></script>");
		}
	}
}
